package com.yupi.springbootinit;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.yupi.springbootinit.utils.MapUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class DistanceCalculator {

    private static final String DIRECTION_API = "https://restapi.amap.com/v5/direction/driving?";
    //https://restapi.amap.com/v3/geocode/geo?address=北京市朝阳区阜通东大街6号&output=XML&key=<用户的key>
    private static final String GEO_API = "https://restapi.amap.com/v3/geocode/geo?output=JSON&";
    private static final String MAP_KEY = "c6cdd7a9d2e0a2cac26954027701e21f";

    public static void main(String[] args) {
        String origin = "114.41843,23.03843";
        String destination = "114.385689,23.076714";
        String address = "广东省惠州市惠城区演达大道46号";

        String postUrl = DIRECTION_API +"strategy=2&" + "origin=" + origin + "&destination=" + destination + "&key=" + MAP_KEY;
        String geoUrl = GEO_API + "address=" + address + "&key=" + MAP_KEY;

        String destination1 = MapUtils.getDestination(origin, destination);
        String location1 = MapUtils.getLocation(address);
        System.out.println("destination1: " + destination1);
        System.out.println("location1: " + location1);
//        try {
//            System.out.println("通过地理编码获取经纬度：");
//            String response = sendGetRequest(geoUrl);
//            Gson gson = new Gson();
//
//            // 将JSON字符串转换为JsonObject
//            JsonObject responseObject = gson.fromJson(response, JsonObject.class);
//
//
//            // 获取"paths"数组
//            JsonArray pathsArray = responseObject.getAsJsonArray("geocodes");
//
//            // 由于paths数组的索引从0开始，我们可以直接获取第一个元素
//            JsonElement pathElement = pathsArray.get(0);
//
//            // 将JsonElement转换为JsonObject以获取"distance"
//            JsonObject pathObject = pathElement.getAsJsonObject();
//
//            // 获取"distance"值
//            String location = pathObject.get("location").getAsString();
//
//            // 打印"distance"值
//            System.out.println("location: " + location);
//            System.out.println(response);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        try {
//            System.out.println("通过路径规划获取距离测试：");
//            String response = sendGetRequest(postUrl);
//            Gson gson = new Gson();
//
//            // 将JSON字符串转换为JsonObject
//            JsonObject responseObject = gson.fromJson(response, JsonObject.class);
//
//            // 获取"route"对象
//            JsonObject routeObject = responseObject.getAsJsonObject("route");
//
//            // 获取"paths"数组
//            JsonArray pathsArray = routeObject.getAsJsonArray("paths");
//
//            // 由于paths数组的索引从0开始，我们可以直接获取第一个元素
//            JsonElement pathElement = pathsArray.get(0);
//
//            // 将JsonElement转换为JsonObject以获取"distance"
//            JsonObject pathObject = pathElement.getAsJsonObject();
//
//            // 获取"distance"值
//            String distance = pathObject.get("distance").getAsString();
//
//            // 打印"distance"值
//            System.out.println("Distance: " + distance);
//            System.out.println(response);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    private static String sendGetRequest(String url) throws IOException {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");

        int responseCode = con.getResponseCode();
        System.out.println("Sending GET request to URL: " + url);
        System.out.println("Response Code: " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        return response.toString();
    }
}
